<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增会议室申请记录')" />
    <th:block th:include="include :: select2-css" />
    <th:block th:include="include :: datetimepicker-css" />
    <style>
        .select2-container--default .select2-selection--multiple {
            border: 1px solid #e5e6e7;
            min-height: 34px;
            border-radius: 1px;
        }
        .select2-container--default .select2-selection--multiple .select2-selection__choice {
            background-color: #1ab394;
            border: 1px solid #1ab394;
            color: #fff;
            border-radius: 3px;
            padding: 3px 8px;
            margin: 4px 5px;
        }
        .select2-container--default .select2-selection--multiple .select2-selection__choice__remove {
            color: #fff;
            margin-right: 5px;
        }
        .select2-container--default .select2-selection--multiple .select2-selection__choice__remove:hover {
            color: #e6e6e6;
        }
        .select2-dropdown {
            border: 1px solid #e5e6e7;
            border-radius: 1px;
        }
        .select2-container--default .select2-search--dropdown .select2-search__field {
            border: 1px solid #e5e6e7;
        }
        .select2-container--default .select2-results__option--highlighted[aria-selected] {
            background-color: #1ab394;
        }
    </style>
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-application-add">
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label is-required">会议室ID：</label>
                    <div class="col-sm-8">
                        <input name="roomId" class="form-control" type="text" required>
                    </div>
                </div>
            </div>
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label is-required">申请部长ID：</label>
                    <div class="col-sm-8">
                        <input name="ministerId" class="form-control" type="text" required>
                    </div>
                </div>
            </div>
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label">申请时间：</label>
                    <div class="col-sm-8">
                        <div class="input-group date">
                            <input name="applicationTime" class="form-control" placeholder="yyyy-MM-dd" type="text">
                            <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label">会议主题：</label>
                    <div class="col-sm-8">
                        <input name="meetingSubject" class="form-control" type="text">
                    </div>
                </div>
            </div>
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label">会议开始时间：</label>
                    <div class="col-sm-8">
                        <div class="input-group date">
                            <input name="meetingStartTime" class="form-control" placeholder="yyyy-MM-dd HH:mm" type="text">
                            <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label">会议结束时间：</label>
                    <div class="col-sm-8">
                        <div class="input-group date">
                            <input name="meetingEndTime" class="form-control" placeholder="yyyy-MM-dd HH:mm" type="text">
                            <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                        </div>
                    </div>
                </div>
            </div>

            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label">参会人员：</label>
                    <div class="col-sm-8">
                        <select id="attendees" name="attendees" class="form-control select2-multiple" multiple>
                            <option value="">请选择参会人员</option>
                        </select>
                        <input type="hidden" id="selectedNames" name="selectedNames">
                    </div>
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: select2-js" />
    <th:block th:include="include :: datetimepicker-js" />
    <script th:inline="javascript">
        var prefix = ctx + "system/application";
        var employeeMap = {};

        $(function() {
            // 初始化select2
            $('.select2-multiple').select2({
                placeholder: "请选择参会人员",
                allowClear: true,
                language: "zh-CN",
                width: '100%',
                theme: 'bootstrap',
                closeOnSelect: false,
                templateResult: formatEmployee,
                templateSelection: formatEmployeeSelection
            });

            // 自定义选项的显示格式
            function formatEmployee(employee) {
                if (!employee.id) return employee.text;
                return $('<span>' + employee.text + '</span>');
            }

            // 自定义已选择项的显示格式
            function formatEmployeeSelection(employee) {
                if (!employee.id) return employee.text;
                return employee.text;
            }

            // 监听部长ID输入框的变化
            $("input[name='ministerId']").change(function() {
                loadEmployees();
            });

            // 加载部门员工的函数
            function loadEmployees() {
                var ministerId = $("input[name='ministerId']").val();
                if (!ministerId) {
                    $.modal.alertWarning("请先选择部长");
                    return;
                }

                $.ajax({
                    url: prefix + "/selectEmployees",
                    type: "post",
                    dataType: "json",
                    data: {
                        "ministerId": ministerId
                    },
                    success: function(res) {
                        if (res.code == 0 && res.data) {
                            employeeMap = {};
                            var html = '<option value="">请选择参会人员</option>';
                            $.each(res.data, function(i, employee) {
                                employeeMap[employee.employeeId] = employee.employeeName;
                                html += '<option value="' + employee.employeeId + '">' + employee.employeeName + '</option>';
                            });
                            $("#attendees").html(html).trigger('change');
                        } else {
                            $.modal.alertWarning(res.msg || "未找到员工数据");
                        }
                    },
                    error: function() {
                        $.modal.alertError("获取员工数据失败");
                    }
                });
            }

            // 监听select2的change事件，更新显示的员工姓名
            $("#attendees").on('change', function() {
                var selectedIds = $(this).val();
                if (selectedIds) {
                    var selectedNames = selectedIds.map(function(id) {
                        return employeeMap[id] || id;
                    });
                    $("#selectedNames").val(selectedNames.join(','));
                } else {
                    $("#selectedNames").val('');
                }
            });
        });

        $("#form-application-add").validate({
            focusCleanup: true
        });

        function submitHandler() {
            if ($.validate.form()) {
                $.operate.save(prefix + "/add", $('#form-application-add').serialize());
            }
        }

        $("input[name='applicationTime']").datetimepicker({
            format: "yyyy-mm-dd",
            minView: "month",
            autoclose: true
        });

        $("input[name='meetingStartTime']").datetimepicker({
            format: "yyyy-mm-dd hh:ii",
            minView: 0,
            minuteStep: 1,
            autoclose: true,
            todayBtn: true,
            startView: 2,
            forceParse: false,
            showMeridian: false,
            language: "zh-CN"
        });

        $("input[name='meetingEndTime']").datetimepicker({
            format: "yyyy-mm-dd hh:ii",
            minView: 0,
            minuteStep: 1,
            autoclose: true,
            todayBtn: true,
            startView: 2,
            forceParse: false,
            showMeridian: false,
            language: "zh-CN"
        });
    </script>
</body>
</html>